package com.yuandian.middle.platform.contoller;

import com.yuandian.api.middle_platform.po.OrgAbilityParam;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.middle.platform.service.IIndicatorDataService;
import com.yuandian.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 说明：能力或服务指标数据查询controller
 * @Author tianlr
 * @Date 2023/5/18 10:11
 */
@Api(value = "indicatorData", tags = "能力或服务指标数据查询接口")
@RestController
@RequestMapping("/modules/indicatorData")
public class IndicatorDataController {
    @Autowired
    private IIndicatorDataService indicatorDataService;

    /**
     * 查询能力或服务对应的指标数据
     * @return
     */
    @PostMapping("/queryDataIndicator")
    @ApiOperation(value = "查询能力或服务对应的指标数据", notes = "查询能力或服务对应的指标数据，当查询能力对应的指标时，必填参数：startTime、endTime、abilityCode、invocationMode;" +
            "当查询服务对应的指标时，必填参数：startTime、endTime、abilityCode、serviceCode、invocationMode")
    public R queryDataIndicator(@RequestBody OrgAbilityParam param) {
        if (param == null || StringUtils.isEmpty(param.getAbilityCode()) || StringUtils.isEmpty(param.getInvocationMode())
                || StringUtils.isEmpty(param.getStartTime()) || StringUtils.isEmpty(param.getEndTime())) {
            return R.failed("传入参数不正确！abilityCode和invocationMode、startTime、endTime不能为空");
        }
        if ("API".equals(param.getInvocationMode()) || "SDK".equals(param.getInvocationMode())) {
            return indicatorDataService.selectDataByInvocationModeApiSdk(param);
        } else if ("PaaS".equals(param.getInvocationMode()) || "SaaS".equals(param.getInvocationMode())) {
            return indicatorDataService.selectDataByInvocationModePaaSaas(param);
        } else if ("H5".equals(param.getInvocationMode())) {
            return indicatorDataService.selectDataByInvocationModeH5(param);
        }
        return R.failed("传入参数不正确！invocationMode值错误");
    }

    /**
     * 查询能力或服务对应的流量趋势数据
     * @return
     */
    @PostMapping("/queryDataFlow")
    @ApiOperation(value = "查询能力服务对应的流量趋势数据", notes = "查询能力服务对应的流量趋势数据，当查询能力对应的指标时，必填参数：startTime、endTime、abilityCode;" +
            "当查询服务对应的指标时，必填参数：startTime、endTime、abilityCode、serviceCode")
    public R queryDataFlow(@RequestBody OrgAbilityParam param) {
        if (param == null || StringUtils.isEmpty(param.getAbilityCode())
                || StringUtils.isEmpty(param.getStartTime()) || StringUtils.isEmpty(param.getEndTime())) {
            return R.failed("传入参数不正确！abilityCode和startTime、endTime不能为空");
        }
        return indicatorDataService.selectDataFlow(param);
    }

    /**
     * 查询能力或服务对应的响应量趋势数据
     * @param param
     * @return
     */
    @PostMapping("/queryDataResponse")
    @ApiOperation(value = "查询能力或服务对应的响应量趋势数据", notes = "查询能力或服务对应的响应量趋势数据，当查询能力对应的指标时，必填参数：startTime、endTime、abilityCode;" +
            "当查询服务对应的指标时，必填参数：startTime、endTime、abilityCode、serviceCode")
    public R queryDataResponse(@RequestBody OrgAbilityParam param) {
        if (param == null || StringUtils.isEmpty(param.getAbilityCode())
                || StringUtils.isEmpty(param.getStartTime()) || StringUtils.isEmpty(param.getEndTime())) {
            return R.failed("传入参数不正确！abilityCode和startTime、endTime不能为空");
        }
        return indicatorDataService.selectDataResponse(param);
    }

    /**
     * 查询服务对应的调用量/核心页面浏览量/浏览量趋势数据
     * @param param
     * @return
     */
    @PostMapping("/queryDataReqCountTrend")
    @ApiOperation(value = "查询服务对应的调用量/核心页面浏览量/浏览量趋势数据", notes = "查询服务对应的响应量趋势数据，必填参数：startTime、endTime、abilityCode、serviceCode、invocationMode")
    public R queryDataReqCountTrend(@RequestBody OrgAbilityParam param) {
        if (param == null || StringUtils.isEmpty(param.getAbilityCode()) || StringUtils.isEmpty(param.getInvocationMode())
                || StringUtils.isEmpty(param.getStartTime()) || StringUtils.isEmpty(param.getEndTime())
                || StringUtils.isEmpty(param.getServiceCode())) {
            return R.failed("传入参数不正确！abilityCode和invocationMode、startTime、endTime、serviceCode不能为空");
        }
        if ("API".equals(param.getInvocationMode()) || "SDK".equals(param.getInvocationMode())) {
            return indicatorDataService.selectDataReqCountTrend(param);
        } else if ("PaaS".equals(param.getInvocationMode()) || "SaaS".equals(param.getInvocationMode())) {
            return indicatorDataService.selectDataCorePageViewTrend(param);
        } else if ("H5".equals(param.getInvocationMode())) {
            return indicatorDataService.selectDataPageViewTrend(param);
        }
        return R.failed("传入参数不正确！invocationMode值错误");
    }

    /**
     * 查询响应耗时分布
     * @param param
     * @return
     */
    @PostMapping("/queryDataResponseTimeDistribution")
    @ApiOperation(value = "查询响应耗时分布", notes = "查询响应耗时分布，必填参数：startTime、endTime、abilityCode、serviceCode")
    public R queryDataResponseTimeDistribution(@RequestBody OrgAbilityParam param) {
        if (param == null || StringUtils.isEmpty(param.getAbilityCode()) || StringUtils.isEmpty(param.getServiceCode())
                || StringUtils.isEmpty(param.getStartTime()) || StringUtils.isEmpty(param.getEndTime())) {
            return R.failed("传入参数不正确！startTime、endTime、abilityCode、serviceCode不能为空");
        }
         return indicatorDataService.selectResponseTimeDistribution(param);
    }

    /**
     * 查询能力或服务对应的响应量快照
     * @param param
     * @return
     */
    @PostMapping("/queryDataResponseSnapshot")
    @ApiOperation(value = "查询能力或服务对应的响应量快照", notes = "查询能力或服务对应的响应量快照，当查询能力对应的指标时，必填参数：startTime、endTime、abilityCode;" +
            "当查询服务对应的指标时，必填参数：startTime、endTime、abilityCode、serviceCode")
    public R queryDataResponseSnapshot(@RequestBody OrgAbilityParam param) {
        if (param == null || StringUtils.isEmpty(param.getAbilityCode())
                || StringUtils.isEmpty(param.getStartTime()) || StringUtils.isEmpty(param.getEndTime())) {
            return R.failed("传入参数不正确！abilityCode和startTime、endTime不能为空");
        }
        return indicatorDataService.selectDataResponseSnapshot(param);
    }
}
